/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: CompositeFilter.java 345 2007-02-05 22:44:50Z jack.rybicki $
 */
package logmatcher.filters;

import logmatcher.InputFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * Filter container which is itself a filter. Included filters can be checked in two modes: first mode -
 * "fallthrough" - one of specified tests needs to be passed; second mode - all test have to be passed.
 *
 * @version $Revision: 345 $
*/
public abstract class CompositeFilter implements InputFilter {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    /** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(CompositeFilter.class);

    //~ Instance fields ------------------------------------------------------------------------------------------------

    /**
     * If <code>false</code> then filtering fails on first failed test, otherwise filtering is continued until
     * some test passes (success) or filter list end is reached (failure).
     */
    private boolean fallThrough;

    /** List of included filters. */
    private List<InputFilter> filters = new ArrayList<InputFilter>();

    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
     * Creates a new CompositeFilter object.
     */
    public CompositeFilter() {
        this(true);
    }


/**
     * Creates a new CompositeFilter object.
     *
     * @param fallThrough if <code>false</code> then filtering fails on first failed test, otherwise filtering
     * is continued until some test passes (success) or filter list end is reached (failure).   
     */
    public CompositeFilter(boolean fallThrough) {
        this.fallThrough = fallThrough;
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     * Get list of filters - used by child classes.
     *
     * @return List of <code>InputFilter</code> objects.
     */
    protected List<InputFilter> getFilters() {
        return filters;
    }


    /**
     * Set list of filters - used by child classes.
     *
     * @param filters List of <code>InputFilter</code> objects to be used in filterring.
     */
    protected void setFilters(List<InputFilter> filters) {
        this.filters = filters;
    }


    /**
     * Set mode: fall-through or first-to-go.
     *
     * @param fallthrough <code>false</code> for fall-through mode, <code>true</code> otherwise.
     */
    protected void setFallThrough(boolean fallthrough) {
        this.fallThrough = fallthrough;
    }


    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    protected boolean getFallThrough() {
        return fallThrough;
    }


    /**
     * @see logmatcher.InputFilter#accept(java.lang.Object)
     */
    public boolean accept(Object message) {
        for (Iterator<InputFilter> it = filters.iterator(); it.hasNext();) {
            InputFilter filter = it.next();

            if (!fallThrough) {
                if (!filter.accept(message)) {
                    return false;
                }
            } else {
                if (filter.accept(message)) {
                    return true;
                }
            }
        }

        return fallThrough ? false : true;
    }
}
